using System;
using System.Text.RegularExpressions;
using Quiki.ExpressionParsers;

namespace Quiki.ElementFormatters
{
    public class ImageDataParser
    {
        public ImageData Parse (WikiImageElement element)
        {
            ImageData imageData = new ImageData();
            imageData.Name = element.Name;

            for (int i = 0; i < element.ImageParameters.Count; i++)
            {
                string parameter = element.ImageParameters[i];

                if (widthRegex.IsMatch(parameter))
                {
                    imageData.Width = parameter;
                    continue;
                }

                if (i == element.ImageParameters.Count - 1)
                    imageData.Caption = parameter;
            }

            return imageData;
        }

        private Regex widthRegex = new Regex(@".+px", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    }
}